import Router from 'koa-router'
const multer = require('koa-multer');
const path = require('path');

const router = new Router();

const storage = multer.diskStorage({
    destination : 'view/resources/',
    filename(ctx,file,cb){
        const filenameArr = file.originalname.split('.');
        cb(null,Date.now() + '.' + filenameArr[filenameArr.length-1]);
    }
});

const upload = multer({storage});

router.post('/file', upload.single('file'), async(ctx)=>{
    return {
        filename: "http://" + ctx.request.header.host + '/resources/' + ctx.req.file.filename
    }
})

export default router
